package com.mis.clothespos.holdOrder.controller;

import com.mis.clothespos.common.AjaxResult;
import com.mis.clothespos.common.BaseController;
import com.mis.clothespos.holdOrder.domain.HoldOrder;
import com.mis.clothespos.holdOrder.domain.HoldOrdertoItem;
import com.mis.clothespos.holdOrder.domain.Holdorderitem;
import com.mis.clothespos.holdOrder.domain.VueHoldOrder;
import com.mis.clothespos.holdOrder.service.HoldService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/HoldOrder")
public class HoldController extends BaseController{

    @Autowired
    HoldService holdService;

    @RequestMapping("/addHoldOrder")
    @ResponseBody
    public AjaxResult addHoldOrder(@RequestBody HoldOrder holdOrder){
        holdOrder.setId(RandomStringUtils.randomAlphanumeric(10));
        holdService.insert(holdOrder);
        AjaxResult result=new AjaxResult();
        result.put("msg","添加成功");
        result.put("code",200);
        return  result;

    }

    @RequestMapping("/selectHoldOrder")
    @ResponseBody
    public AjaxResult selectHoldOrder(){

        AjaxResult result= new AjaxResult();
        result.put("holdOrder",holdService.select());
        result.put("code",200);
        result.put("msg","查询成功");
        return  result;
    }

    @GetMapping("/selectHoldOrderByPhone")
    @ResponseBody
    public AjaxResult selectHoldOrderByPhone(HoldOrder order){
        AjaxResult result =new AjaxResult();
        List<HoldOrder> holdOrder=holdService.selectByPhone(order);
        result.put("holdOrder",holdOrder);
        result.put("code",200);
        result .put("msg","查询成功");
        result.put("total",holdOrder.size());
        return  result;
    }

    @GetMapping("/selectHoldOrderById")
    @ResponseBody
    public  AjaxResult selectHoldOrderById(HoldOrder order){
        AjaxResult result =new AjaxResult();
        List<Holdorderitem> ItemList=holdService.selectItemById(order.getId());
        result.put("itemList",ItemList);
        result.put("code",200);
        result .put("msg","查询成功");
        return  result;
    }

    @PostMapping("/addHoldOrder")
    @ResponseBody
    public AjaxResult addHoldOrder(@RequestBody VueHoldOrder vueHoldOrder){
        AjaxResult result =new AjaxResult();
        HoldOrder holdOrder = new HoldOrder();
        holdOrder.setId(RandomStringUtils.randomAlphanumeric(10));
        holdOrder.setName(vueHoldOrder.getName());
        holdOrder.setNote(vueHoldOrder.getNote());
        holdOrder.setPhone(vueHoldOrder.getPhone());
        holdOrder.setTime(new Date());
        holdService.insert(holdOrder);
        for(int i=0;i<vueHoldOrder.getItem().size();i++){
            Holdorderitem holdorderitem=vueHoldOrder.getItem().get(i);
            holdorderitem.setItemid(RandomStringUtils.randomAlphanumeric(10));
            //插入挂单条目
            holdService.inserHoldOrderitem(holdorderitem);

            HoldOrdertoItem holdOrdertoItem=new HoldOrdertoItem();
            holdOrdertoItem.setHoldorderid(holdOrder.getId());
            holdOrdertoItem.setHoldorderitemid(holdorderitem.getItemid());
            //插入挂单条目和挂单中间表
            holdService.insertHoldOrderToItem(holdOrdertoItem);
        }
        result.put("code",200);
        result.put("msg","挂单成功");




        return  result;
    }

    @PostMapping("/deleteHoldOrderById")
    @ResponseBody
    public AjaxResult deleteHoldOrderById(@RequestBody HoldOrder order) {
        holdService.deleteByPrimaryKey(order.getId());
        AjaxResult result =new AjaxResult();
        result.put("code", 200);
        result.put("message", "删除成功");
        return  result;
    }
}
